package com.ophiux.cs.infrastructure.repositories.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ophiux.cs.domain.user.domainservice.UserDomainService;
import com.ophiux.cs.domain.user.model.entity.User;
import com.ophiux.cs.domain.user.model.valueobj.UserValueObj;
import com.ophiux.cs.domain.user.repositories.IUserRepository;
import com.ophiux.cs.infrastructure.compoment.basedata.mybatis.DataComponent;
import com.ophiux.cs.infrastructure.mapper.user.UserMapper;
import com.ophiux.utils.common.DateUtil;
import com.ophiux.utils.exception.OphiuxRepositoryException;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class UserRepository implements IUserRepository {
	@Autowired
	UserMapper userMapper;
	@Autowired
	DataComponent dataComponent;
	@Autowired
	UserDomainService userDomainService;
	
	@Override
	@Transactional//开启事务回滚
	public List<User> getUserByUserAcc(String userAcc) throws OphiuxRepositoryException{
		List<User> userList = null;
		try {
			userList =  userMapper.getUserByUserAcc(userAcc);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("getUserByUserAcc仓储异常",e);
			throw new OphiuxRepositoryException("getUserByUserAcc仓储异常",e);
		}
		return userList;
	}
	
	

	@Override
	public User getUser(UserValueObj obj) throws OphiuxRepositoryException {
		User user = null;
		try {
			user =  userMapper.getUser(obj);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("getUserByUserAcc仓储异常",e);
			throw new OphiuxRepositoryException("getUserByUserAcc仓储异常",e);
		}
		return user;
	}


	@SuppressWarnings("static-access")
	@Override
	@Transactional//开启事务回滚
	public boolean savaUser(User userInfo){
		try {
			String userId = userDomainService.getUserId();
			userInfo.setUSER_ID(userId);
			userInfo.setSTATUS(User.static_success);
			userInfo.setREG_DATE(DateUtil.getCurDateStr());
			userMapper.addUser(userInfo);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			log.info("savaUser仓储异常",e);
			return false;
		}
	}

	@Override
	public User getUserByUserId(String USER_ID)  throws OphiuxRepositoryException{
		User user = null;
		try {
			user = userMapper.getUserByUserId(USER_ID);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("getUserByUserId仓储异常",e);
			throw new OphiuxRepositoryException("getUserByUserId仓储异常",e);
		}
		return user;
	}

	@Override
	public boolean upUserByUserId(User userInfo)   throws OphiuxRepositoryException{
		try {
			userMapper.upUserByUserId(userInfo);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			log.info("upUserByUserId仓储异常",e);
			throw new OphiuxRepositoryException("upUserByUserId仓储异常",e);
		}
	}


}
